Implement Flyweight design pattern

class BetterFormattedText{
public:
struct TextRange{
int start, end;
bool capitalized;
// bool
bool converse(int position) const {
return position>=start && position<=end;
}
};
TextRange& get_range(int start, int end){
formatting.emplace_back(TextRange{start, end});
return *formatting.rbegin();
}
private:
string plain_text;
vector<TextRange> formatting;
};
friend std::ostream& operator<<(std::ostream& os, const BetterFormattedText& obj){
strint s;
for(size_t i=0; i<obj.plain_text.length(); i++){
auto c=obj.plain_text[i];
for(const auto& rng: obj.formatting){
if(rng.covers(i) && rng.capitalize) c=toupper(c);
s+=c;
}
}
return os<<s;
}
//
BetterFormattedText bft("This is a brave new world");
bft.get_range(10, 15).capitalize=true;
cout<<bft<<endl;
플라이웨이트 패턴은 기본적으로 공간 절약을 위한 테크닉